package com.oocl.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.oocl.connection.MQConnection;
import com.oocl.pojo.Book;
import com.oocl.producer.MsgProducer;
import com.oocl.producer.impl.MsgProducerImpl;
import com.oocl.util.FileIOUtil;
import com.oocl.util.JacksonUtil;
import com.oocl.util.StringRandomUtil;

public class MsgProducerManager {

	private MsgProducer producer;
	private static MsgProducerManager sInstance = new MsgProducerManager();
	
	private List<Book> bookList;
	
	private MsgProducerManager(){
		
		this.producer = new MsgProducerImpl();
	}
	
	public static MsgProducerManager getInstance(){
		return sInstance;
	}
	
	
	public void initData(){
//		saveBooksToFile(10000);
		this.bookList = (List<Book>) FileIOUtil.readObject("books.dat");
	}
	
	
	
	
	public void startSendMsgs(int num){
		if(this.bookList ==null || this.bookList.size()==0){
			System.out.println("没有可用数据");
			return;
		}
		///生产者上线
		this.producer.onLine();
		
		int i=0;
		for(Book book : bookList){
			String jsonStr = JacksonUtil.fromObjectToJson(book);
			System.out.println("准备发送： "+jsonStr);
			producer.sendMsg(jsonStr);
			i++;
			if(num!=-1 && i>=num)
				break;
		}
		
		///生产者下线
		this.producer.offLine();
		
		///连接断开
		MQConnection.getInstance().close();
	}

}
